package com.sisgesbyte.dao.tecnico;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.beanDao.MenuEditRequest;
import com.sisgesbyte.dao.beanDao.MenuNuevoRequest;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.tecnico.ListaMenuEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoMenuDaoImpl extends AbstractFacade<ListaMenuEntity>
		implements MantenimientoMenuDao {

	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;

	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}

	public MantenimientoMenuDaoImpl() {
		super(ListaMenuEntity.class);
	}

	@Override
	public List<ListaMenuEntity> listaMenu(FiltrosSQLBean cadenaFiltros) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select me.id, "
				+ "me.modulo_id as codmodulo, "
				+ "mo.nombre as modulo, "
				+ "me.parent_id as codpadre, "
				+ "(select men.nombre as nombrepadre from tecnico.menu men where men.id= me.parent_id) , "
				+ "me.nombre as menu, " + "me.icon, " + "me.orden, "
				+ "me.url, " + "me.active ");
		jpql.append("from tecnico.menu me inner join tecnico.modulo mo on me.modulo_id = mo.id ");
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		Query query = em.createNativeQuery(jpql.toString(), ListaMenuEntity.class);
		// Seteamos los parametros del fitro
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<ListaMenuEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity nuevoMenu(MenuNuevoRequest menu) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from tecnico.sp_registrar_menu (:in_moduloId, :in_padre, :in_menu, :in_icon, :in_orden, :in_url, :in_active )");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_moduloId", menu.getModuloId());
		query.setParameter("in_padre", menu.getPadre());
		query.setParameter("in_menu", menu.getNombre());
		query.setParameter("in_icon", menu.getIcon());
		query.setParameter("in_orden", menu.getOrden());
		query.setParameter("in_url", menu.getUrl());
		query.setParameter("in_active", menu.getActive());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarMenu(MenuEditRequest menu) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT * from tecnico.sp_editar_menu(:in_menuid,:in_moduloId, :in_parentId, :in_menu, :in_icon, :in_orden, :in_url, :in_active) ");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_menuid", menu.getCodMenu());
		query.setParameter("in_moduloId", menu.getModuloId());
		query.setParameter("in_parentId", menu.getPadre());
		query.setParameter("in_menu", menu.getNombre());
		query.setParameter("in_icon", menu.getIcon());
		query.setParameter("in_orden", menu.getOrden());
		query.setParameter("in_url", menu.getUrl());
		query.setParameter("in_active", menu.getActive());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarMenu(int codigo) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT * from tecnico.sp_eliminar_menu(:codMenu)");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("codMenu", codigo);

		return (GenericEntity) query.getSingleResult();
	}

}
